import os
from celery import Celery
from .PdfProcessor import PdfProcessor
from .TxtProcessor import TxtProcessor
from Logger import Logger
from ConfigManager import ConfigManager
from fulltext.Document import Document
from celery.events import EventReceiver

logger = Logger.get_logger(__name__)

# Initialize Celery app
redis_broker = ConfigManager().get("redis_broker")
redis_backend = ConfigManager().get("redis_backend")
app = Celery("docs_tasks", 
             broker=redis_broker, backend=redis_backend)

@app.task(base=PdfProcessor)
def process_document(messages: dict) -> bool:
    """
    Celery task to process and index a document

    Args:
        document: Document instance to process

    Returns:
        bool: True if successful, False otherwise
    """
    file_path = messages.get("file_path")
    _, ext = os.path.splitext(file_path)
    # 判断ext类型做不同的处理
    match ext:
        case ".pdf":
            processor = PdfProcessor()
            processor.run(messages)
        case ".docx":
            pass
        case ".txt":
            processor = TxtProcessor()
            processor.run(messages)
        case _:
            pass
    return True
